<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    chartData: {
      type: Array,
      default: function() {
        return [];
      }
    }
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      const chartData = this.chartData;
      const title = this.chartData[0].title;
      // const nameList = this.chartData.reduce((pVal, cVal) => {
      //   const target = pVal.find(x => x.name === cVal.name);
      //   if (!target) {
      //     pVal.push(cVal.name);
      //   }
      //   return pVal;
      // }, []);
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: title,

          left: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        // legend: {
        //   left: "center",
        //   bottom: "10",
        //   data: nameList
        // },
        series: [
          {
            name: "时效",
            type: "pie",
            // roseType: "radius",
            radius: "50%",
            // radius: [15, 95],
            // center: ["50%", "50%"],

            // roseType: "area",
            // itemStyle: {
            //   borderRadius: 8
            // },
            data: chartData,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      });
    }
  }
};
</script>
